#coding:utf8
from config import *

mouse_checking_l = (1,3,4)

def add_and_check(x, y):
    cfg.mouse_m_left_dis[0] += x
    cfg.mouse_m_left_dis[1] += y
    a =  cfg.mouse_m_left_dis[0]
    b =  cfg.mouse_m_left_dis[1]    
    dd = math.sqrt(a*a+b*b)    
    if  dd> 6:
        cfg.mouse_mode = 1
        cfg.mouse_m_left_fix_pos = copy.deepcopy(cfg.mouse_m_left_dis)
        cfg.mouse_m_left_fix_time = time.time()
        
def get_m_du_inner(x1,y1,dx, dy):
    d1 = math.atan2(y1, x1)
    d2 = math.atan2(dy, dx)
    d = d1 - d2
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    return abs(d)

def get_m_du(dx, dy):
    x1, y1 =  cfg.mouse_m_left_fix_pos[0], cfg.mouse_m_left_fix_pos[1]
    return get_m_du_inner(x1,y1,dx,dy)
    
def add_and_check2(x, y):
    if cfg.mouse_mode==3 and cfg.mouse_m_right_dis[0]==0 and\
            cfg.mouse_m_right_dis[1]==0:
        cfg.mouse_mode =4
        cfg.mouse_mode4_pos = [0,0]
        cfg.mouse_mode4_right_time = 0
        cfg.mouse_mode4_pos[0] +=x
        cfg.mouse_mode4_pos[1] +=y
        cfg.mouse_m_left_fix_time = time.time()
        a,b = cfg.fix_pos
        x = 3*cfg.mouse_m_int_stop[0]-2*a
        y = 3*cfg.mouse_m_int_stop[1]-2*b
        cfg.mouse_m_dst_stop = (x,y)
        return
    
    if time.time() - cfg.mouse_m_left_fix_time > 0.8:
        print('set -1 p4')
        cfg.mouse_mode = -1
        return
    if cfg.mouse_mode==4:
        cfg.mouse_mode4_pos[0] +=x
        cfg.mouse_mode4_pos[1] +=y

    cfg.mouse_m_left_dis[0] += x
    cfg.mouse_m_left_dis[1] += y
    a, b =  cfg.mouse_m_left_max
    c, d =  cfg.mouse_m_left_dis    
    if c *c +d *d > a * a + b * b:
        cfg.mouse_m_left_max = copy.deepcopy(cfg.mouse_m_left_dis)
        if math.sqrt(c *c +d *d) > 250:
            print('set -1 p6')
            cfg.mouse_mode = -1
            return        
    else:
        dx =  cfg.mouse_m_left_max[0] - cfg.mouse_m_left_dis[0] 
        dy =  cfg.mouse_m_left_max[1] - cfg.mouse_m_left_dis[1]
        
        dd = 10
        if cfg.mouse_mode==4:
            dd = 100
        if math.sqrt(dx*dx+dy*dy) > dd:            
            d = get_m_du(-dx, -dy)
            print('max r', -dx, -dy, d)
            if d > math.pi * 0.5:                
                cfg.mouse_mode = -1
                print('set -1 p5', dx, dy)
                return            
    
def add_and_check_right(x, y):     
    if cfg.mouse_mode==4 and cfg.mouse_mode4_right_time==0:
        cfg.mouse_mode4_right_time = time.time()
    cfg.mouse_m_right_dis[0] += x
    cfg.mouse_m_right_dis[1] += y
    a =  cfg.mouse_m_right_dis[0]
    b =  cfg.mouse_m_right_dis[1]
    if math.sqrt(a*a+b*b) <= 10:
        return
    
    if cfg.mouse_mode==4:        
        d = get_m_du_inner(cfg.mouse_mode4_pos[0],cfg.mouse_mode4_pos[1],a,b)
        if d>math.pi * 0.5 and cfg.mouse_mode4_right_time-cfg.mouse_m_left_fix_time<0.15:
            print('hit2',cfg.mouse_mode4_right_time-cfg.mouse_m_left_fix_time)
            cfg.mouse_mode = 2
            return
        
    d = get_m_du(a, b)    
    if (d > math.pi * 0.5 and cfg.mouse_mode in (1,3)) or\
        (d <= math.pi * 0.5 and not cfg.mouse_mode in (1,3)):
        cfg.mouse_mode = 2
        return
    print('set -1 p7')
    cfg.mouse_mode = -1
          
    
def deal_mouse_mode(h, x, y):
    if time.time() - cfg.clear_time < 0.02 or time.time() -cfg.move_task_time<0.2:
        return
    if cfg.mouse_mode in (-1, 2):        
        return
    
    if h in cfg.left_mouse:
        cfg.mouse_m_left_last_time = time.time()
        
    if cfg.mouse_mode == 0:
        if h not in cfg.left_mouse:
            print('set -1 p3')
            cfg.mouse_mode = -1
            return
        
        if cfg.mouse_m_left_start_time == 0:
            print('set start', x, y)
            cfg.mouse_m_left_start_time = time.time()
            
        add_and_check(x, y)
        return
    
    if h in cfg.left_mouse:
        add_and_check2(x, y)
        return
    add_and_check_right(x, y)

def check_mouse_mode():
    if cfg.mouse_mode in (-1, 2):        
        return    
    
    if cfg.mouse_mode == 0:
        if cfg.mouse_m_left_start_time == 0:
            return
            
        if time.time() - cfg.mouse_m_left_start_time > 0.5:
            print('set -1 p1')
            cfg.mouse_mode = -1
            return
        return
    gg = 0.15
    if cfg.mouse_mode==1 and time.time()-cfg.left_mouse_time>gg and \
        time.time()-cfg.right_mouse_time>gg and cfg.mouse_m_right_dis[0]==0 and\
            cfg.mouse_m_right_dis[1]==0:
        cfg.mouse_mode = 3
        cfg.mouse_m_left_fix_time  = time.time()
        cfg.mouse_m_int_stop = get_cur_pos()
        return
    
    d =time.time() - cfg.mouse_m_left_fix_time     
    
    if  d> 2:
        print('set -1 p2', d)
        cfg.mouse_mode = -1
        return            
